package com.sandu.network.service;


import com.sandu.network.Interceptor.BaseInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by clark on 2024/11/15.
 */

public class ApiClient {
    public static String baseUrl = "";
    public static String gankHost = "";
    public static String dynamicHost = "";


    public static void initHost(String baseurl, String gankhost, String dynamichost, String... url) {
        baseUrl = baseurl;
        gankHost = gankhost;
        dynamicHost = dynamichost;

    }

    //添加 reset事务
    public static void resetApi() {
        retrofitInstance = null;
//        retrofitInstanceAIBody = null;
//        retrofitInstanceAIFace = null;
        okHttpClientInstance = null;
//        okHttpClientInstanceAI = null;
    }

    /**
     * 获取指定数据类型
     *
     * @return
     */
    public static <T> T getGankDataService(Class<T> serviceClass) {

        T gankDataService = initService(gankHost, serviceClass);

        return gankDataService;
    }

//    /**
//     * 获取指定数据类型
//     *
//     * @return
//     */
//    public static GankDataService getGankAIFaceDataService() {
//
//        GankDataService gankDataService = initAIFaceService(ApiConstants.AIFaceHost, GankDataService.class);
//
//        return gankDataService;
//    }
//
//    public static GankDataService getGankAIBodyDataService() {
//
//        GankDataService gankDataService = initAIBodyService(ApiConstants.AIBodyHost, GankDataService.class);
//
//        return gankDataService;
//    }

    /**
     * 动态url获取数据
     *
     * @return
     */
    public static DynamicApiService getDynamicDataService() {

        DynamicApiService dynamicApiService = ApiClient.initService("", DynamicApiService.class);

        return dynamicApiService;
    }

    /**
     * 获得想要的 retrofit service
     *
     * @param baseUrl 数据请求url
     * @param clazz   想要的 retrofit service 接口，Retrofit会代理生成对应的实体类
     * @param <T>     api service
     * @return
     */
    private static <T> T initService(String baseUrl, Class<T> clazz) {
        return getRetrofitInstance(baseUrl).create(clazz);
    }


    /**
     * 获得想要的 retrofit service
     *
     * @param baseUrl 数据请求url
     * @param clazz   想要的 retrofit service 接口，Retrofit会代理生成对应的实体类
     * @param <T>     api service
     * @return
     */
//    private static <T> T initAIFaceService(String baseUrl, Class<T> clazz) {
//        return getRetrofitInstanceAIFace(baseUrl).create(clazz);
//    }
//
//    private static <T> T initAIBodyService(String baseUrl, Class<T> clazz) {
//        return getRetrofitInstanceAIBody(baseUrl).create(clazz);
//    }

    /**
     * 单例retrofit
     */
    private static Retrofit retrofitInstance;

    private static Retrofit getRetrofitInstance(String baseUrl) {
        if (retrofitInstance == null) {
            synchronized (ApiClient.class) {
                if (retrofitInstance == null) {
                    retrofitInstance = new Retrofit.Builder()
                            .baseUrl(gankHost)
                            .addConverterFactory(GsonConverterFactory.create())
                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                            .client(getOkHttpClientInstance())
                            .build();
                }
            }
        }
        return retrofitInstance;
    }


    /**
     * 单例OkHttpClient
     */
    private static OkHttpClient okHttpClientInstance;

    private static OkHttpClient getOkHttpClientInstance() {
        if (okHttpClientInstance == null) {
            synchronized (ApiClient.class) {
                if (okHttpClientInstance == null) {
                    OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
//                    if (BuildConfig.DEBUG) {
                    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
                    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    builder.addInterceptor(httpLoggingInterceptor);
                    builder.addInterceptor(new BaseInterceptor(null));
                    builder.connectTimeout(150, TimeUnit.MILLISECONDS).writeTimeout(150, TimeUnit.MILLISECONDS);//设置超时
//                      builder.addNetworkInterceptor(new StethoInterceptor());
//                      BuildConfig.STETHO.addNetworkInterceptor(builder);
//                    }
                    okHttpClientInstance = builder.build();
                }
            }
        }
        return okHttpClientInstance;
    }


    /**
     * 单例retrofit
     */
//    private static Retrofit retrofitInstanceAIFace;
//
//    private static Retrofit getRetrofitInstanceAIFace(String baseUrl) {
//        if (retrofitInstanceAIFace == null) {
//            synchronized (ApiClient.class) {
//                if (retrofitInstanceAIFace == null) {
//                    retrofitInstanceAIFace = new Retrofit.Builder()
//                            .baseUrl(ApiConstants.AIFaceHost)
//                            .addConverterFactory(GsonConverterFactory.create())
//                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                            .client(getOkHttpClientInstanceAI())
//                            .build();
//                }
//            }
//        }
//        return retrofitInstanceAIFace;
//    }

    /**
     * 单例retrofit
     */
//    private static Retrofit retrofitInstanceAIBody;
//
//    private static Retrofit getRetrofitInstanceAIBody(String baseUrl) {
//        if (retrofitInstanceAIBody == null) {
//            synchronized (ApiClient.class) {
//                if (retrofitInstanceAIBody == null) {
//                    retrofitInstanceAIBody = new Retrofit.Builder()
//                            .baseUrl(ApiConstants.AIBodyHost)
//                            .addConverterFactory(GsonConverterFactory.create())
//                            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
//                            .client(getOkHttpClientInstanceAI())
//                            .build();
//                }
//            }
//        }
//        return retrofitInstanceAIBody;
//    }

    /**
     * 单例OkHttpClient
     */
//    private static OkHttpClient okHttpClientInstanceAI;

//    private static OkHttpClient getOkHttpClientInstanceAI() {
//        if (okHttpClientInstanceAI == null) {
//            synchronized (ApiClient.class) {
//                if (okHttpClientInstanceAI == null) {
//                    OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
////                    if (BuildConfig.DEBUG) {
//                    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
//                    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//                    builder.addInterceptor(httpLoggingInterceptor);
//                    builder.addInterceptor(new BaseInterceptor(null));
//                    builder.connectTimeout(150, TimeUnit.MILLISECONDS).writeTimeout(150, TimeUnit.MILLISECONDS);//设置超时
////                      builder.addNetworkInterceptor(new StethoInterceptor());
////                      BuildConfig.STETHO.addNetworkInterceptor(builder);
////                    }
//                    okHttpClientInstanceAI = builder.build();
//                }
//            }
//        }
//        return okHttpClientInstanceAI;
//    }

}
